package cn.com.guanying.android.ui.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;
import cn.com.guanying.R;


public class GuanyingDialog extends Dialog implements android.view.Window.Callback {
	private final int MIDDLEVIEW = 1;

	private TextView mTextViewTitle;
	private TextView mTextViewInfo = null;
	private ImageView mImageViewIcon = null;

	private Button mButton1 = null;
	private Button mButton2 = null;
	private Button mButton3 = null;

	private View mLayoutTop;
	private View mLayoutMiddle = null;
	private View mLayoutBottom = null;

	private RelativeLayout mMilldeView = null;
	private RelativeLayout contentLayout;

	private View mDialogView;

	public GuanyingDialog(Context context) {
		super(context, R.style.dialog);
		setContentView(R.layout.dialog_guanyingdialog);
		setTitle("提示");
		getWindow()
				.setFlags(
						WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES
								| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
						WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES
								| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		refreshDialogView();
	}

	public void setInfo(String str) {
		setMiddleViewVisibility(true);
		if (mMilldeView != null) {
			mMilldeView = (RelativeLayout) findViewById(R.id.dialog_feitionInfoView);
			mMilldeView.removeViewAt(MIDDLEVIEW);
		}
		mTextViewInfo = (TextView) findViewById(android.R.id.text1);
		mTextViewInfo.setVisibility(View.VISIBLE);
		mTextViewInfo.setText(str);

	}

	public void setInfo(int strRes) {
		setInfo(getContext().getString(strRes));
	}

	public void setInfo(View view) {
		setMiddleViewVisibility(true);

		if (mMilldeView == null)
			mMilldeView = (RelativeLayout) findViewById(R.id.dialog_feitionInfoView);
		LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		params.addRule(RelativeLayout.CENTER_HORIZONTAL);
		mMilldeView.addView(view, MIDDLEVIEW, params);
	}

	public void setView(View view) {
		contentLayout = (RelativeLayout) findViewById(R.id.dialog_feitionInfoLayout);
		contentLayout.setVisibility(RelativeLayout.VISIBLE);
		contentLayout.removeAllViews();
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT);
		contentLayout.addView(view, params);
	}

	public void setTitle(String str) {

		setTopViewVisibility(true);
		mTextViewTitle = (TextView) findViewById(android.R.id.title);
		mTextViewTitle.setVisibility(View.VISIBLE);
		mTextViewTitle.setText(str);
	}

	public void setTitle(int strRes) {
		setTitle(getContext().getString(strRes));
	}

	public void setIcon(int res) {
		setTopViewVisibility(true);
		mImageViewIcon = (ImageView) findViewById(android.R.id.icon);
		mImageViewIcon.setVisibility(View.VISIBLE);
		mImageViewIcon.setImageResource(res);
	}

	public void setButton1(String str, View.OnClickListener listener) {
		setBottomViewVisibility(true);
		mButton1 = (Button) findViewById(android.R.id.button1);
		if (str != null)
			mButton1.setText(str);
		mButton1.setVisibility(View.VISIBLE);
		if (listener != null)
			mButton1.setOnClickListener(listener);
	}

	public void setButton1(int strRes, View.OnClickListener listener) {
		setButton1(getContext().getString(strRes), listener);
	}

	public void setButton1Enabled(boolean enabled) {
		setBottomViewVisibility(true);
		if (mButton1 == null)
			mButton1 = (Button) findViewById(android.R.id.button1);
		mButton1.setEnabled(enabled);
		mButton1.setBackgroundResource(R.drawable.button_disable);
	}

	public void setButton2Enabled(boolean enabled) {
		setBottomViewVisibility(true);
		if (mButton2 == null)
			mButton2 = (Button) findViewById(android.R.id.button1);
		mButton2.setEnabled(enabled);
		mButton2.setBackgroundResource(R.drawable.button_disable);
	}

	public void setButton3Enabled(boolean enabled) {
		setBottomViewVisibility(true);
		if (mButton3 == null)
			mButton3 = (Button) findViewById(android.R.id.button1);
		mButton3.setEnabled(enabled);
		mButton3.setBackgroundResource(R.drawable.button_disable);
	}

	public void setButton2(String str, View.OnClickListener listener) {
		setBottomViewVisibility(true);
		mButton2 = (Button) findViewById(android.R.id.button2);
		if (str != null)
			mButton2.setText(str);
		mButton2.setVisibility(View.VISIBLE);
		if (listener != null)
			mButton2.setOnClickListener(listener);
	}

	public void setButton2(int strRes, View.OnClickListener listener) {
		setButton2(getContext().getString(strRes), listener);
	}

	public void setButton3(String str, View.OnClickListener listener) {
		setBottomViewVisibility(true);
		mButton3 = (Button) findViewById(android.R.id.button3);
		if (str != null)
			mButton3.setText(str);
		mButton3.setVisibility(View.VISIBLE);
		if (listener != null)
			mButton3.setOnClickListener(listener);
	}

	public void setButton3(int strRes, View.OnClickListener listener) {
		setButton3(getContext().getString(strRes), listener);
	}

	private void setTopViewVisibility(boolean visible) {
		if (mLayoutTop == null)
			mLayoutTop = findViewById(R.id.dialog_feitionTitleLayout);
		mLayoutTop.setVisibility((visible ? View.VISIBLE : View.GONE));
	}

	private void setMiddleViewVisibility(boolean visible) {

		if (mLayoutMiddle == null)
			mLayoutMiddle = findViewById(R.id.dialog_feitionInfoLayout);
		mLayoutMiddle.setVisibility((visible ? View.VISIBLE : View.GONE));
	}

	private void setBottomViewVisibility(boolean visible) {

		if (mLayoutBottom == null)
			mLayoutBottom = findViewById(R.id.dialog_feitionBottomLayout);
		mLayoutBottom.setVisibility((visible ? View.VISIBLE : View.GONE));
	}

	// 在dialog被show后如果有对dialog进行修改，务必在修改之后调用refreshDialogView()
	public void refreshDialogView() {
		mDialogView = findViewById(R.id.dialog_view);
	}

	// 当用户从最小化状态回来后让其强制刷新UI
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		if (mDialogView != null)
			setContentView(mDialogView);
	}
}
